<template>
  <div class="app-container">
    <div class="menu-container" style="margin-bottom: 10px; float: right">
      <el-button
        v-waves
        type="primary"
        icon="el-icon-edit"
        @click="onClickCreateRole"
      >
        {{ $t("RoleManager.createRole") }}
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column
        :label="$t('RoleManager.id')"
        align="center"
        width="180px"
      >
        <template slot-scope="{ row }">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('RoleManager.role')"
        width="180px"
        align="center"
      >
        <template slot-scope="{ row }">
          <span> {{ row.roleName }}</span>
        </template>
      </el-table-column>

      <el-table-column
        :label="$t('RoleManager.remarks')"
        width="580px"
        align="center"
      >
        <template slot-scope="{ row }">
          <span>{{ row.remarks }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('RoleManager.operation')"
        align="center"
        width="280px"
      >
        <template slot-scope="{ row }">
          <el-button size="mini" type="primary" @click="onClickEdit(row)"
            >编辑</el-button
          >
          <!-- <el-button size="mini" type="danger" @click="onClickDelete(row)"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <el-dialog
      center
      :title="getDialogTitle()"
      width="30%"
      :visible.sync="dialogFormVisible"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="tempShowData"
        style="width: 90%"
      >
        <el-form-item label="角色名称" label-width="100px">
          <el-input
            v-model="tempShowData.roleName"
            type="text"
            placeholder="请输入角色名称"
            clearable
            maxlength="10"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="备注说明" label-width="100px">
          <el-input
            v-model="tempShowData.remarks"
            type="textarea"
            placeholder="请输入角色名称"
            clearable
            maxlength="50"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="角色权限" label-width="100px">
          <div
            v-for="(groupItem, index) in mainGroupList"
            :key="groupItem.id"
            style="margin-bottom: 10px"
          >
            <el-checkbox
              :indeterminate="isIndeterminate[index]"
              v-model="checkAllPrivacy[index]"
              @change="handleCheckAllChange(groupItem, index)"
              >{{ groupItem.groupname }}</el-checkbox
            >
            <el-checkbox-group
              v-model="checkedList[index]"
              @change="handleCheckedOneChange(index)"
            >
              <el-checkbox
                v-for="data in getSubGroupList(groupItem.group)"
                :label="data"
                :key="data.id"
                >{{ data.subtypename }}</el-checkbox
              >
            </el-checkbox-group>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogFormVisible = false">
          {{ $t("table.cancel") }}
        </el-button>
        <el-button type="primary" @click="onSureEdit()">
          {{ $t("table.confirm") }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import Pagination from "@/components/Pagination/index.vue";
import { PrivacyConfig } from "@/config/PrivacyConfig";
import { RoleModel } from "@/store/model/RoleModel";
import { Logger } from "@/utils/Logger";
import { Form } from "element-ui";
import { Component, Vue } from "vue-property-decorator";
import SplitPane from "../components-demo/split-pane.vue";

@Component({
  name: "rolemanager",
  components: {
    Pagination,
    SplitPane,
  },
})
export default class extends Vue {
  private tableKey = 0;
  private list: RoleModel[] = [];
  private total = 0;
  private listLoading = true;

  private dialogFormVisible = false; //dialog是否显示

  private channelId: number = 0;

  private downloadLoading = false;

  //列表查询选项
  private listQuery = {
    page: 1, //第几页
    limit: 20, //一页多少个
    sort: "+id", //排序方式
  };

  private tempShowData: any = {};
  private rules = {};

  private DialogStateType = {
    Create: 0, //创建
    Edit: 1, //修改
  };

  private dialogState: number;

  private mainGroupList: Array<PrivacyConfig> = [];

  private checkAllPrivacy: Array<boolean> = [];
  private checkedList: Array<Array<PrivacyConfig>> = [];

  private isIndeterminate: Array<boolean> = [];

  mounted() {
    this.mainGroupList = PrivacyConfig.getMainGroupList();
    this.reInitDialogInfo();
    this.getList();
  }

  private reInitDialogInfo() {
    for (let i = 0; i < this.mainGroupList.length; i++) {
      this.checkedList[i] = [];
      this.isIndeterminate[i] = false;
      this.checkAllPrivacy[i] = false;
    }
  }

  private getCheckAll(index: number) {
    return this.checkAllPrivacy[index];
  }

  private getSubGroupList(group: number) {
    let list: Array<PrivacyConfig> = PrivacyConfig.getSubGroupList(group);
    return list;
  }

  private handleCheckedOneChange(index: number) {
    let subGroupList: Array<PrivacyConfig> = PrivacyConfig.getSubGroupList(
      index + 1
    );

    if (this.checkedList[index].length >= subGroupList.length) {
      this.checkAllPrivacy[index] = true;
      this.isIndeterminate[index] = false;
    } else {
      this.checkAllPrivacy[index] = false;
      if (this.checkedList[index].length > 0) {
        this.isIndeterminate[index] = true;
      } else {
        this.isIndeterminate[index] = false;
      }
    }
    this.$forceUpdate(); //这里要强制刷新vue界面，防止界面刷新bug
  }

  private handleCheckAllChange(groupItem, index) {
    if (this.checkAllPrivacy[index]) {
      this.checkedList[index] = PrivacyConfig.getSubGroupList(index + 1);
    } else {
      this.checkedList[index] = [];
    }
    this.isIndeterminate[index] = false;
  }

  private getDialogTitle() {
    if (this.dialogState == this.DialogStateType.Create) {
      return "创建角色";
    } else {
      return "编辑角色";
    }
  }

  private onClickCreateRole() {
    this.reInitDialogInfo();
    this.dialogFormVisible = true;
    this.dialogState = this.DialogStateType.Create;
    this.$nextTick(() => {
      (this.$refs.dataForm as Form).clearValidate(); // 移除所有表单项的校验
    });
  }

  private onClickEdit(model: RoleModel) {
    this.tempShowData = Object.assign({}, model);
    this.dialogState = this.DialogStateType.Edit;
    this.dialogFormVisible = true;
    Logger.log("onClickEdit==", this.tempShowData);
    this.$nextTick(() => {
      (this.$refs.dataForm as Form).clearValidate(); // 移除所有表单项的校验
    });
    this.reInitDialogInfo();
    for (let i = 0; i < model.privacyIdList.length; i++) {
      let privacyConfig: PrivacyConfig = PrivacyConfig.configMap.get(
        model.privacyIdList[i]
      );
      if (privacyConfig) {
        this.checkedList[privacyConfig.group - 1].push(privacyConfig);
        this.handleCheckedOneChange(privacyConfig.group-1);
      } else {
        Logger.error("找不到权限管理配置==", model.privacyIdList[i]);
      }
    }
  }

  private onClickDelete(model: RoleModel) {
    Logger.log("onClickDelete==", model);
  }

  private onSureEdit() {
    Logger.log("onSureEdit==", this.tempShowData);
    this.dialogFormVisible = false;
  }

  private async getList() {
    this.listLoading = true;
    RoleModel.getShowList((result: Array<RoleModel>) => {
      this.list = result;
      this.listLoading = false;
    });
  }
}
</script>

<style lang="scss">
// .app-container {
//   .menu-container {
//     display: block;
//   }
// }
</style>